library(tidyverse)
library(ggplot2)

util_path        <- "code/utils"
data_path        <- "output/data_generated"
plot_path        <- "output/plots"

source(sprintf("%s/format_plots.R", util_path))

main <- function() {

    estimation_df <- read.csv(sprintf("%s/estimation_outputs.csv", data_path))
    estimation_df$Distance <- estimation_df$Distance * 100
    
    yref <- estimation_df %>% filter(Model == "L", 
                                     Version == "original",
                                     MisspecType == "X_only", 
                                     Outcome == "mean_own_elasticity", 
                                     Instrument == "price as instrument", 
                                     Distance_group == 11)
    
    generate_plots(
      df = estimation_df %>%
        filter(Model == "L", 
               Version == "original",
                                     MisspecType == "X_only", 
               Outcome == "mean_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments")
        ),
      yref = yref,
      ylab = "Median bias of estimated\nmean own-price elasticity", 
      xlab = "Distance from correct specification of mean utility", 
      ybreaks = seq(-0.5, 2.5, 0.5), xbreaks = seq(0, 0.5, 0.1),
      file_path = sprintf("%s/medbias_logit_varying_X.pdf", plot_path)
    )

    generate_plots(
      df = estimation_df %>%
        filter(Model == "L", 
               Version == "original",
               MisspecType == "X_only", 
               Outcome == "mean_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments", 
                                 "partial residualization (products)")
        ),
      yref = yref,
      ylab = "Median bias of estimated\nmean own-price elasticity", 
      xlab = "Distance from correct specification of mean utility", 
      ybreaks = seq(-0.5, 2.5, 0.5),  xbreaks = seq(0, 0.5, 0.1),
      file_path = sprintf("%s/medbias_logit_varying_X_partial_resid.pdf", plot_path)
    )

    generate_plots(
      df = estimation_df %>%
        filter(Model == "L", 
               Version == "original",
               MisspecType == "X_and_D", 
               Outcome == "mean_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments")
        ),
      yref = yref,
      ylab = "Median bias of estimated\nmean own-price elasticity", 
      xlab = "Distance from causally correct specification (lower bound)", 
      ybreaks = seq(-0.5, 2.5, 0.5),  xbreaks = seq(0, 0.01, 0.002),
      file_path = sprintf("%s/medbias_logit_varying_D.pdf", plot_path)
    )

    generate_plots(
      df = estimation_df %>%
        filter(Model == "L", 
               Version == "original",
               MisspecType == "X_and_D", 
               Outcome == "mean_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments", 
                                 "partial residualization (products)")
        ),
      yref = yref,
      ylab = "Median bias of estimated\nmean own-price elasticity", 
      xlab = "Distance from causally correct specification (lower bound)", 
      ybreaks = seq(-0.5, 2.5, 0.5), xbreaks = seq(0, 0.01, 0.002),
      file_path = sprintf("%s/medbias_logit_varying_D_partial_resid.pdf", plot_path)
    )
    
    generate_plots(
      df = estimation_df %>%
        filter(Model == "L_nm", 
               Version == "original",
               MisspecType == "X_and_D", 
               Outcome == "mean_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments", 
                                 "partial residualization (products)")
        ),
      yref = yref,
      ylab = "Median bias of estimated\nmean own-price elasticity", 
      xlab = "Distance from causally correct specification (lower bound)", 
      ybreaks = seq(-0.5, 2.5, 0.5), xbreaks = seq(0, 0.01, 0.002),
      file_path = sprintf("%s/medbias_dropmonth_varying_D.pdf", plot_path)
    )

    generate_plots(
      df = estimation_df %>%
        filter(Model == "L", 
               Version == "original",
               MisspecType == "X_only", 
               Outcome == "mean_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments","product-level residualization")
        ),
      yref = yref,
      ylab = "Median bias of estimated\nmean own-price elasticity", 
      xlab = "Distance from correct specification of mean utility", 
      ybreaks = seq(-0.5, 2.5, 0.5), xbreaks = seq(0, 0.5, 0.1),
      file_path = sprintf("%s/medbias_logit_varying_X_prod_resd.pdf", plot_path),
      type = "product"
    )

    generate_plots(
      df = estimation_df %>%
        filter(Model == "L", 
               Version == "original",
               MisspecType == "X_and_D", 
               Outcome == "mean_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments","product-level residualization")
        ),
      yref = yref,
      ylab = "Median bias of estimated\nmean own-price elasticity", 
      xlab = "Distance from causally correct specification (lower bound)", 
      ybreaks = seq(-0.5, 2.5, 0.5),  xbreaks = seq(0, 0.01, 0.002),
      file_path = sprintf("%s/medbias_logit_varying_D_prod_resd.pdf", plot_path),
      type = "product"
    )

    generate_plots(
      df = estimation_df %>%
        filter(Model == "L", 
               Version == "shelf",
               MisspecType == "X_and_D", 
               Outcome == "mean_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments","product-level residualization")
        ),
      yref = yref,
      ylab = "Median bias of estimated\nmean own-price elasticity", 
      xlab = "Distance from causally correct specification (lower bound)", 
      ybreaks = seq(-0.5, 2.5, 0.5),  xbreaks = seq(0, 0.01, 0.002),
      file_path = sprintf("%s/medbias_logit_varying_D_prod_resd_sh.pdf", plot_path),
      type = "product"
    )


    yref <- estimation_df %>% slice(0)

    generate_plots(
      df = estimation_df %>%
        filter(Model == "RCL23", 
               Version == "original",
               MisspecType == "X_only", 
               Outcome == "mean_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments")
        ),
      yref = yref,
      ylab = "Median bias of estimated\nmean own-price elasticity", 
      xlab = "Distance from correct specification of mean utility", 
      ybreaks = seq(-0.5, 2.5, 0.5), xbreaks = seq(0, 0.5, 0.1),
      file_path = sprintf("%s/medbias_rcl23_varying_X.pdf", plot_path)
    )

    
    generate_plots(
      df = estimation_df %>%
        filter(Model == "RCL23", 
               Version == "original",
               MisspecType == "X_and_D", 
               Outcome == "mean_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments")
        ),
      yref = yref,
      ylab = "Median bias of estimated\nmean own-price elasticity", 
      xlab = "Distance from causally correct specification (lower bound)", 
      ybreaks = seq(-0.5, 2.5, 0.5), xbreaks = seq(0, 0.0015, 0.0003),
      file_path = sprintf("%s/medbias_rcl23_varying_D.pdf", plot_path)
    )
    
    yref <- estimation_df %>% filter(Model == "L", 
                                     Version == "original",
                                     MisspecType == "X_only", 
                                     Outcome == "mad_own_elasticity", 
                                     Instrument == "price as instrument", 
                                     Distance_group == 11)

    generate_plots(
      df = estimation_df %>%
        filter(Model == "L", 
               Version == "original",
               MisspecType == "X_only", 
               Outcome == "mad_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments", 
                                 "partial residualization (products)","product-level residualization")
        ),
      yref = yref,
      ylab = "MAE of estimated\nmean own-price elasticity", 
      xlab = "Distance from correct specification of mean utility", 
      ybreaks = seq(-0.5, 2.5, 0.5), xbreaks = seq(0, 0.5, 0.1),
      file_path = sprintf("%s/mae_logit_varying_X_partial_resid.pdf", plot_path)
    )

    generate_plots(
      df = estimation_df %>%
        filter(Model == "L",
               Version == "original",
               MisspecType == "X_and_D", 
               Outcome == "mad_own_elasticity",
               Instrument %in% c("strongly excluded instruments", "miller and weinberg instruments", 
                                 "partial residualization (products)","product-level residualization")
        ),
      yref = yref,
      ylab = "MAE of estimated\nmean own-price elasticity", 
      xlab = "Distance from causally correct specification (lower bound)", 
      ybreaks = seq(-0.5, 2.5, 0.5), xbreaks = seq(0, 0.01, 0.002),
      file_path = sprintf("%s/mae_logit_varying_D_partial_resid.pdf", plot_path)
    )

}

main()

